import { baseURL } from "@/env"
import { request } from "@/utils/requst"

Page({
    data: {
        formData: {
            description: '',
            bugImage: [],
            contactWay: '',
        },
        fileList: []
    },
    afterRead(e) {
        console.log(e.detail.file.url)
        this.uploadtoServer(e.detail.file.url)
    },
    // 上传到头像到服务器
    uploadtoServer(url: string) {
        const that = this
        wx.uploadFile({
            url: `${baseURL}/upload/image`, // 仅为示例，非真实的接口地址
            filePath: url,
            name: 'file', // 文件对应的 key，开发者在服务端可以通过这个 key 获取文件的二进制内容
            header: {
                Authorization: `Bearer ${wx.getStorageSync('token')}`
            },
            success(res) {
                const result: any = JSON.parse(res.data)
                const img = {
                    url: `${baseURL}/${result.data.path}`,
                    name: result.data.originalname,
                    type: 'image'
                }
                const fileList = that.data.fileList
                fileList.push(img)
                that.setData({
                    fileList
                })
                //   that.setData({
                //         avatarUrl: `${baseURL}/${result.data.path}`
                //     })
                // do something
            }
        })
    },
    async handleSubmit() {
        const formData = this.data.formData
        formData.bugImage = this.data.fileList
        this.setData({
            formData
        })
        if (formData.description === '') {
            wx.showToast({
                icon: 'error',
                title: '问题描述为必填噢',
            })
            return 
        }
       const result:any = await request('POST','/upload/bug',formData)       
        if (result.data.status) {
            wx.showToast({
                title: '感谢您的意见',
                icon: 'success'
            })
            wx.navigateBack()
        }
    },
    handleDEscr(e){
        const formData = this.data.formData
        formData.description = e.detail.value
        this.setData({
            formData
        })
    },
    handleContactWay(e){
        const formData = this.data.formData
        formData.contactWay = e.detail.value
        this.setData({
            formData
        })
    }
})